package com.otaku.order.service.impl;

import com.otaku.common.ResultUtils.ResultModel;
import com.otaku.common.ResultUtils.ResultUtil;
import com.otaku.memberAPI.entity.UserEntity;
import com.otaku.order.dao.MemberDao;
import com.otaku.order.entity.MemberEntity;
import com.otaku.order.feign.FeignMemberService;
import com.otaku.order.interfaceApi.SendMailInterface;
import com.otaku.orderAPI.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

@Api("订单服务中心")
@RestController
public class OrderServiceImpl implements IOrderService {

    private final static Logger LOGGER = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Autowired
    private FeignMemberService feignMemberService;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private SendMailInterface sendMailInterface;


    /**
     * 将服务放在消息队列中异步处理
     * @return
     */
    @RequestMapping("/sendManage")
    public String sendManage(){
        String msg = UUID.randomUUID().toString();
        LOGGER.info("order生产者发送的信息：{}",msg);
        String msgMail = "mail"+msg;
        Message<byte[]> build = MessageBuilder.withPayload(msgMail.getBytes()).build();
        sendMailInterface.sendMail().send(build);
        sendMailInterface.sendMsg().send(build);
        return "success";
    }


    @RequestMapping("/getOrder")
    public String getOrder(String name) {
        UserEntity entity = feignMemberService.getMember(name);
        return entity.toString();
    }

    @RequestMapping("/getMemberInfo")
    public String getMemberInfo(String name) {
        String str = feignMemberService.getMemberInfo();
        return str;
    }

    @RequestMapping("/getMemberInfoHystrix")
    public String getMemberInfoHystrix(String name) {
        String str = feignMemberService.getMemberInfoHystrix();
        return str;
    }

    @ApiOperation("swaggerInfo 测试")
    @GetMapping("/swaggerInfo")
    public String swaggerInfo(String name) {
        return "name:" + name;
    }

    /**
     * fb 权限不足
     * 成功
     *
     * @return
     */
    @RequestMapping("/getError")
    public ResultModel getError() {
        ResultModel msg = feignMemberService.getSysError();
        return msg;
    }

    /**
     * 成功
     * Json解析错误
     *
     * @return
     */
    @RequestMapping("/getError2")
    public ResultModel getError2() {
        ResultModel msg = feignMemberService.getSysError2();
        return msg;
    }

    @RequestMapping("/insertDB")
    public ResultModel insertDB() {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setId(123);
        memberEntity.setAge(20);
        memberEntity.setName("ORDER");
        int insert = memberDao.insert(memberEntity);
        LOGGER.info("保存成功：{}条数据", insert);
        return ResultUtil.success();
    }



}
